﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Move_User_AttAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string pronum = Session["pro_num"].ToString();
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            //已经添加的其他属性
            string tabelall = "<table class='layui-table'><thead> <tr><td colspan='8' style='background-color: #EEE;font-size:large;text-align:center'>该设备的其他属性</td></tr><tr><th>属性名称</th><th>属性值</th></tr></thead>";
            SqlCommand cmdmsg = new SqlCommand();
            string SQLmsg = "select keyword,pro_value from Attribute where att_num=@pro_num";
            cmdmsg.Connection = conn;
            cmdmsg.CommandText = SQLmsg;
            cmdmsg.CommandType = CommandType.Text;
            //提交参数 SQL 语句中必须要有@参数
            SqlParameter ppro_num = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
            ppro_num.Value = pronum;
            cmdmsg.Parameters.Add(ppro_num);
            using (SqlDataReader readermsg = cmdmsg.ExecuteReader())
            {
                int fieldcount = readermsg.FieldCount;      //取字段总数
                if (!readermsg.HasRows)            //有数据  ---行
                {
                    MsgKey.Text = "<table class='layui-table' style='width: 70%; margin-left:15% '><tr><td>此仪器没有被添加过其他属性</td></tr>";
                }
                else
                {
                    string trd = "<tbody>";                     //存储数据
                    while (readermsg.Read())               //当有数据可读时
                    {
                        trd += "<tr>";         //数据行开始，行以tr起
                        for (int i = 0; i < fieldcount; i++)
                        {   //每一列的内容
                            trd += "<td>" + readermsg[i].ToString() + "</td>";
                        }
                    }
                    MsgKey.Text = tabelall + trd + "</tbody></table>";
                }
            }

        }
        finally
        {
            conn.Close();
        }
    }


    protected void BtAdd_Click(object sender, EventArgs e)
        {
            string keyword = FKey.Text.Trim();
            string provalue = FValue.Text.Trim();
            string pronum = Session["pro_num"].ToString();
            string proname = "";

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
            try
            {
                conn.Open();
                //读仪器名称
                string SQLname = "select pro_name from Products where pro_num=@pronum";
                SqlCommand cmdname = new SqlCommand();
                cmdname.Connection = conn;
                cmdname.CommandType = CommandType.Text;
                cmdname.CommandText = SQLname;
                SqlParameter p1 = new SqlParameter("@pronum", pronum);
                cmdname.Parameters.Add(p1);
                //cmdimg.ExecuteNonQuery();
                proname = cmdname.ExecuteScalar().ToString();


                string SQL = "insert into Attribute (keyword,pro_value,att_num,att_name) values (@keyword,@pro_value,@att_num,@att_name)";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL;

                //依次添加每个字段的值
                SqlParameter pkey = new SqlParameter("@keyword", SqlDbType.NVarChar, 20);
                pkey.Value = keyword;
                cmd.Parameters.Add(pkey);

                SqlParameter pvalue = new SqlParameter("@pro_value", SqlDbType.NVarChar, 100);
                pvalue.Value = provalue;
                cmd.Parameters.Add(pvalue);

                SqlParameter ppronum = new SqlParameter("@att_num", SqlDbType.NVarChar, 10);
                ppronum.Value = pronum;
                cmd.Parameters.Add(ppronum);

                SqlParameter pproname = new SqlParameter("@att_name", SqlDbType.NVarChar, 20);
                pproname.Value = proname;
                cmd.Parameters.Add(pproname);

                int R = cmd.ExecuteNonQuery();  //R 的值就是返回影响表的行数
                if (R > 0)
                {
                    Session["Message"] = "添加属性成功，该仪器有独属于你的属性了！";
                    Session["ReturnPath"] = "Move_User_AttView.aspx";
                    Response.Redirect("Success.aspx");

                }
                else
                {
                    Session["Message"] = "添加属性失败！";
                    Response.Redirect("Error.aspx");
                }

                //已经添加的其他属性
                string tabelall = "<table class='layui-table'><thead> <tr><td colspan='8' style='background-color: #EEE;font-size:large;text-align:center'>该设备的其他属性</td></tr><tr><th>属性名称</th><th>属性值</th></tr></thead>";
                SqlCommand cmdmsg = new SqlCommand();
                string SQLmsg = "select att_id,keyword,pro_value from Attribute where att_num=@pro_num";
                cmdmsg.Connection = conn;
                cmdmsg.CommandText = SQLmsg;
                cmdmsg.CommandType = CommandType.Text;
                //提交参数 SQL 语句中必须要有@参数
                SqlParameter ppro_num = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
                ppro_num.Value = pronum;
                cmdmsg.Parameters.Add(ppro_num);
                using (SqlDataReader readermsg = cmdmsg.ExecuteReader())
                {
                    int fieldcount = readermsg.FieldCount;      //取字段总数
                    if (!readermsg.HasRows)            //有数据  ---行
                    {
                        MsgKey.Text = "<table class='layui-table' style='width: 70%; margin-left:15% '><tr><td>此仪器没有被添加过其他属性</td></tr>";
                    }
                    else
                    {
                        string trd = "<tbody>";                     //存储数据
                        while (readermsg.Read())               //当有数据可读时
                        {
                            trd += "<tr>";         //数据行开始，行以tr起
                            for (int i = 0; i < fieldcount; i++)
                            {   //每一列的内容
                                trd += "<td>" + readermsg[i].ToString() + "</td>";
                            }
                        }
                        MsgKey.Text = tabelall + trd + "</tbody></table>";
                    }
                }

            }
            finally
            {
                conn.Close();
            }
        }

}